﻿using System;
using Xtensive.Orm;

namespace CompositeKey.Model
{
    [HierarchyRoot]
    public class User : Entity
    {
        [Field, Key]
        public int Id { get; private set; }

        [Field]
        public string Name { get; set; }

        public User(Session session)
            : base(session) { }
    }

    [HierarchyRoot]
    public class Group : Entity
    {
        [Field, Key]
        public Guid Id { get; private set; }

        [Field]
        public string Name { get; set; }

        public Group(Session session) :
            base(session) { }
    }

    /*
     * Composite key is declared by [Key(n)] attribute, where n - index of a field in the primary key
     * 
     * For instance, primary key for Membership class in database will be [int, guid] where int goes for User,Id
     * and guid - for Group.Id
     * 
     * All key values must be passed to entity's constructor and to its base constructor in correct order, 
     * so they could be used to build the primary key.
     */
    [HierarchyRoot]
    public class Membership : Entity
    {
        [Field, Key(0)]
        public User User { get; private set; }

        [Field, Key(1)]
        public Group Group { get; private set; }

        public Membership(Session session, User user, Group group) :
            base(session, user, group) { }
    }
}